//
//  BigMapViewController.m
//  Runone
//
//  Created by runone01 on 16/9/8.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "BigMapViewController.h"

#import "Define.h"


@interface BigMapViewController ()<MAMapViewDelegate>
@property(nonatomic,strong)MAPointAnnotation *pointAnnotation;
@end

@implementation BigMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [_mapView removeAnnotation:_pointAnnotation];
    _pointAnnotation = [[MAPointAnnotation alloc] init];
    [self loadMapView];
    if (_stationModel) {
        [self defaultStationAnnotation];
    }
    if (_cameraInfo) {
        [self deafaultCameraAnnotaion];
    }
    
    self.preferredContentSize=CGSizeMake(self.view.frame.size.width-20, self.view.frame.size.height-50);
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-50, 40, 40, 40)];
    [btn addTarget:self action:@selector(backToDevice) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:[UIImage imageNamed:@"suoxiao"] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    // Do any additional setup after loading the view.
}
-(void)backToDevice
{
    [_pointarray removeAllObjects];
    [self.navigationController popViewControllerAnimated:YES];
   // [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)loadMapView
{
    [AMapServices sharedServices].apiKey = GETGAODEPAIKEY;
    
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    _mapView.delegate = self;
    
    _mapView.showsCompass = NO;
    _mapView.showsScale= NO;
    [self.view addSubview:_mapView];
    
}

-(void)defaultStationAnnotation { // 0.247900--0.158443--12.000000--25.228899--113.071313
    
    MACoordinateSpan span = MACoordinateSpanMake(0.247900, 0.158443);
    CLLocationCoordinate2D ret = AMapCoordinateConvert(CLLocationCoordinate2DMake(self.latitude, self.longitude), AMapCoordinateTypeGPS);
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(ret.latitude, ret.longitude);

    MACoordinateRegion region = MACoordinateRegionMake(centerCoordinate, span);
    _mapView.region = region;
    [_mapView setCenterCoordinate:centerCoordinate animated:NO];
    _mapView.zoomLevel = 12.000000;
    NSLog(@"%@",_stationModel.TollStationName);
    
}

- (void) deafaultCameraAnnotaion {
    MACoordinateSpan span = MACoordinateSpanMake(0.247900, 0.158443);
    CLLocationCoordinate2D ret = AMapCoordinateConvert(CLLocationCoordinate2DMake(_cameraInfo.Latitude, _cameraInfo.Longitude), AMapCoordinateTypeGPS);
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(ret.latitude, ret.longitude);
    
    MACoordinateRegion region = MACoordinateRegionMake(centerCoordinate, span);
    _mapView.region = region;
    [_mapView setCenterCoordinate:centerCoordinate animated:NO];
    _mapView.zoomLevel = 12.000000;
}

-(void)loadStationAnnotationView
{
    [_mapView removeAnnotation:_pointAnnotation];
    [_pointarray removeAllObjects];
    _pointarray=[[NSMutableArray alloc] init];
    _pointAnnotation = [[MAPointAnnotation alloc] init];
    float latitude = _stationModel.Latitude ;
    float longitude =_stationModel.Longitude ;
    
    _pointAnnotation.coordinate = AMapCoordinateConvert(CLLocationCoordinate2DMake(latitude, longitude), AMapCoordinateTypeGPS);
    //    pointAnnotation.title = [NSString stringWithFormat:@"%@",self.stationModel.TollStationName];
    _pointAnnotation.title = _Name;
    //    pointAnnotation.subtitle = [NSString stringWithFormat:@"%@",self.stationModel.TollStationName];
    [_mapView addAnnotation:_pointAnnotation];
    [_pointarray addObject:_pointAnnotation];
    NSLog(@"%@",_stationModel.TollStationName);
    [self.mapView showAnnotations:_pointarray edgePadding:UIEdgeInsetsMake(20, 20, 20, 80) animated:YES];
}

-(void)loadCameraAnnotationView
{
    [_pointarray removeAllObjects];
    _pointarray=[[NSMutableArray alloc] init];
   // MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    float latitude = _cameraInfo.Latitude ;
    float longitude =_cameraInfo.Longitude ;
    
    _pointAnnotation.coordinate = AMapCoordinateConvert(CLLocationCoordinate2DMake(latitude, longitude), AMapCoordinateTypeGPS);
    //    pointAnnotation.title = [NSString stringWithFormat:@"%@",self.stationModel.TollStationName];
    _pointAnnotation.title = _Name;
    //    pointAnnotation.subtitle = [NSString stringWithFormat:@"%@",self.stationModel.TollStationName];
    [_mapView addAnnotation:_pointAnnotation];
    [_pointarray addObject:_pointAnnotation];
    [self.mapView showAnnotations:_pointarray edgePadding:UIEdgeInsetsMake(20, 20, 20, 80) animated:YES];
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        if ([_type isEqualToString:@"收费站"]) {
             annotationView.image = [UIImage imageNamed:@"收费站图标@3x.png"];
        }
        if ([_type isEqualToString:@"摄像枪"]) {
             annotationView.image = [UIImage imageNamed:@"摄像枪图标@3x.png"];
        }
        //设置中心点偏移，使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -18);
        return annotationView;
    }
    return nil;
}
-(void)viewDidAppear:(BOOL)animated
{
    if (_stationModel) {
        [self loadStationAnnotationView];
    }
    if (_cameraInfo) {
        [self loadCameraAnnotationView];
    }
    
}

-(void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    NSLog(@"%f--%f--%f--%f--%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta,mapView.zoomLevel,mapView.region.center.latitude,mapView.region.center.longitude);
}

@end
